package jalus;

import jalus.service.ConfigureService;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 负责本blog请求的转发
 *
 */
@SuppressWarnings("serial")
public class DispatcherServlet extends HttpServlet {
	@SuppressWarnings("unchecked")
	Map<String, Class> actionMap = new LinkedHashMap<String, Class>();
	@Override
	public void init() throws ServletException {
		super.init();
		initMapping();
	}
	
	//初始化URL映射
	private void initMapping(){
		//文章
		actionMap.put("/blog/post", jalus.blog.PostServlet.class);
		actionMap.put("/blog", jalus.blog.PostServlet.class);
		//评论
		actionMap.put("/blog/comment", jalus.blog.CommentServlet.class);
		//友情链接
		actionMap.put("/blog/bloglink", jalus.blog.BlogLinkServlet.class);
		//Rss输出
		actionMap.put("(/blog)?/feed", jalus.blog.FeedServlet.class);
		//相册
		actionMap.put("/album", jalus.blog.PicasaServlet.class);
		//后台管理
		actionMap.put("/consle", jalus.blog.ConsleServlet.class);
		//访问统计
		actionMap.put("/statistic", jalus.blog.StatisticServlet.class);
		//文件
		actionMap.put("/(showimage|showfile)/[0-9]+", jalus.blog.FileServlet.class);
		//文件管理
		actionMap.put("/file", jalus.blog.FileManagerServlet.class);
		//feed阅读
		actionMap.put("/reader", jalus.blog.ReaderServlet.class);
		//豆瓣
		actionMap.put("/douban", jalus.blog.DoubanServlet.class);
		//sitemap
		actionMap.put("/sitemap.xml", jalus.blog.SitemapServlet.class);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException {
		try {
			Action servlet = findActionMap(req.getRequestURI());
			if(servlet == null){
				resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
			}else{
				servlet.doGet(req, resp);
			}
		} catch (InstantiationException e) {
			resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
		} catch (IllegalAccessException e) {
			resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			Action servlet = findActionMap(req.getRequestURI());
			if(servlet == null){
				resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
			}else{
				servlet.doPost(req, resp);
			}
		} catch (InstantiationException e) {
			resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
		} catch (IllegalAccessException e) {
			resp.sendRedirect(ConfigureService.getInstance().getErrorPage());
		}
	}
	
	//查找URI对应的处理类
	private Action findActionMap(String uri) throws InstantiationException, IllegalAccessException{
		for(String uriPattern : actionMap.keySet()){
			if(uri.matches(uriPattern)){
				return (Action) actionMap.get(uriPattern).newInstance();
			}
		}
		return null;
	}
}
